#Replication file for "Defending the Dog Whistle#
#This file contains the code necessary to replicate the analyses and results from
#the main paper; a separate replication file does the same for the online appendix.

####Libraries and color palette####
library(ggplot2)
library(interflex)
library(estimatr)
#Set color palette
cbbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

####2019 Data (Study 1)####
####Load in the data####
setwd("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Data/Lucid Denial experiment")
load("Study 1.RData")

####Median time spent on the treatment####
summary((d$treat_time))
nrow(d[d$treat_time==15.30,])/nrow(d[!is.na(d$treat_time),])

####Median time spent on the survey####
summary((d$`Duration (in seconds)`)/60)
nrow(d[d$`Duration (in seconds)`<180,])

####Basic treatment effects####
t1=t.test(d$mayor_evalR[d$treatment<2]~d$treatment[d$treatment<2], data=d[d$treatment<2,])
t2=t.test(d$mayor_insensitiveR[d$treatment<2]~d$treatment[d$treatment<2], data=d[d$treatment<2,])

#indirect vs. indirect + denial
t3=t.test(d$mayor_evalR[d$treatment==2|d$treatment==3]~d$treatment[d$treatment==2|d$treatment==3],
          data=d[d$treatment==2|d$treatment==3,])
t4=t.test(d$mayor_insensitiveR[d$treatment==2|d$treatment==3]~d$treatment[d$treatment==2|d$treatment==3],
          data=d[d$treatment==2|d$treatment==3,])

#direct vs. direct + denial
t5=t.test(d$mayor_evalR[d$treatment==4|d$treatment==5]~d$treatment[d$treatment==4|d$treatment==5],
          data=d[d$treatment==4|d$treatment==5,])
t6=t.test(d$mayor_insensitiveR[d$treatment==4|d$treatment==5]~d$treatment[d$treatment==4|d$treatment==5],
          data=d[d$treatment==4|d$treatment==5,])

####FIGURE 1, STUDY 1####
####
#Approval only
t <- data.frame(treatment=c("Indirect", "Direct",
                            "Indirect", "Direct"),
                var=c("Mayor's proposal","Mayor's proposal",
                      "Mayor's insensitivity","Mayor's insensitivity"),
                dif=c(t3$estimate[2]-t3$estimate[1],
                      t5$estimate[2]-t5$estimate[1],
                      t4$estimate[2]-t4$estimate[1],
                      t6$estimate[2]-t6$estimate[1]
                ), 
                se=c(
                  t3$stderr,
                  t5$stderr,
                  t4$stderr,
                  t6$stderr
                )
)

t$treatment=factor(t$treatment, levels=c("Indirect", "Direct"))
t$var=factor(t$var, levels=c("Mayor's proposal", "Mayor's insensitivity"))

plot1=ggplot(data = t[t$var=="Mayor's proposal",], aes(x=treatment, y=dif, ymin=I(dif-(1.96*se)), ymax=I(dif+(1.96*se)), color=treatment))+
  geom_pointrange(size=1.5, fatten=2, aes(shape=treatment)) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="red") +
  scale_color_manual(values=cbbPalette)+
  theme_classic()+
  theme(axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=14),
        axis.title.y = element_text(size=14),
        plot.title = element_text(size=12))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.position="bottom")+
  theme(legend.key.size = unit(3,"line"))+
  theme(legend.title = element_blank())+
  theme(plot.caption.position = "plot",
        plot.caption = element_text(hjust = 0)) +
  coord_cartesian(ylim = c(-0.10, 0.10))+
  guides(color=guide_legend(keywidth = 2, keyheight = 5))+
  guides(color = guide_legend(nrow = 1))+
  labs(title="", 
       y= "Effect of denials", 
       x="",
       caption = "       Graph presents treatment effects for the indirect and direct racial messages, along 
       with 95 percent confidence intervals. The dependent variable is support for the 
       Mayor's proposed policy, which is a 7 point likert item scaled from 0 (least support) to 
       1 (most support). N is 618 and 600 for the indirect and direct conditions (respectively)")
p=plot1+ggtitle("Support for Mayor's proposal, 2019")
p
setwd("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Final POBE graphs")
ggsave("Figure 1, 2019 Denial - Basic treatment effects approval only.jpeg", width=5, height=6, units="in", dpi=600)

####FIGURE 2, STUDY 1: Interaction with RR####
temp=d[!is.na(d$Implicit_reject),]
m1<-interflex("binning", Y = "mayor_evalR", D = "Implicit_reject", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")
temp=d[!is.na(d$Explicit_reject),]
m2<-interflex("binning", Y = "mayor_evalR", D = "Explicit_reject", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")


Diff=c(m1$est.bin$`1`[1,2],
       m1$est.bin$`1`[2,2],
       m1$est.bin$`1`[3,2],
       m2$est.bin$`1`[1,2],
       m2$est.bin$`1`[2,2],
       m2$est.bin$`1`[3,2]
)

se=c(m1$est.bin$`1`[1,3],
     m1$est.bin$`1`[2,3],
     m1$est.bin$`1`[3,3],
     m2$est.bin$`1`[1,3],
     m2$est.bin$`1`[2,3],
     m2$est.bin$`1`[3,3]
)

rr=c("Low RR", "Medium RR", "High RR",
     "Low RR", "Medium RR", "High RR"
)

t1=data.frame(cbind(Diff, rr))
t1$se=as.numeric(se)
t1$Diff=as.numeric(Diff)

t1$Group=c("Indirect", "Indirect", "Indirect",
           "Direct", "Direct", "Direct"
)
t1$Group=factor(t1$Group, levels=c(
  "Indirect", "Direct"
))
t1$rr=factor(t1$rr, levels=c(
  "Low RR", "Medium RR", "High RR"
))

#These commands do the actual graphing

plot1=ggplot(data = t1, aes(x=Group, y=Diff, ymin=I(Diff-(1.96*se)), ymax=I(Diff+(1.96*se)), color=Group))+
  geom_pointrange(size=1.5, fatten=2, aes(shape=Group)) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="red") +
  scale_color_manual(values=cbbPalette)+
  theme_classic()+
  theme(axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=14),
        axis.title.y = element_text(size=14),
        plot.title = element_text(size=12))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.position="bottom")+
  theme(legend.key.size = unit(3,"line"))+
  theme(legend.title = element_blank())+
  theme(plot.caption.position = "plot",
        plot.caption = element_text(hjust = 0)) +
  coord_cartesian(ylim = c(-0.15, 0.20))+
  scale_y_continuous(breaks=c(-0.15,-0.075,0,0.075, 0.15))+
  guides(color=guide_legend(keywidth = 2, keyheight = 5))+
  guides(color = guide_legend(nrow = 1))+
  labs(title="", 
       y= "Effect of denial", 
       x="",
       caption = "       Graph presents treatment effects for the indirect and direct racial messages, along with 95 percent confidence intervals. The dependent 
       variable is support for the Mayor's proposed policy, which is a 7 point Likert item scaled from 0 (least support) to 1 (most support). 
       N is those low in racial resentment is 201 and 193 for the indirect and direct conditions (respectively). For those with medium levels 
       of racial resentment, N is 207 (indirect) and 209 (direct); N for those high in racial resentment is 202 (indirect) and 188 (direct).")

p=plot1+facet_wrap(vars(rr))+theme(strip.text.x = element_text(size = 12))+ theme(panel.spacing=unit(.05, "lines"),
                                                                                  panel.border = element_rect(color = "black", fill = NA, size = 1), 
                                                                                  strip.background = element_rect(color = "black", size = 1))+
  ggtitle("Approval of mayor's proposal")
p
setwd("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Final POBE graphs")
ggsave("Figure 2, Approval of proposal.jpeg", width=8, height=6, units="in", dpi=600)

####P-values of the differences across levels of RR####
#Indirect
temp=d[!is.na(d$Implicit_reject),]
#Comparing the low and medium RR conditions
t= (m1$est.bin$`1`[2,2]-m1$est.bin$`1`[1,2])/sqrt((m1$est.bin$`1`[2,3]^2+m1$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<3.75,])+nrow(temp[temp$rr_scale>=3.75&temp$rr_scale<=4.5,])-2, lower.tail=F))*2
#p=0.06

#Comparing the low and high RR conditions
t= (m1$est.bin$`1`[3,2]-m1$est.bin$`1`[1,2])/sqrt((m1$est.bin$`1`[3,3]^2+m1$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<3.75,])+nrow(temp[temp$rr_scale>4.5,])-2, lower.tail=F))*2
#p=0.05

#Direct
temp=d[!is.na(d$Explicit_reject),]
#comparing low RR to medium RR
t= (m2$est.bin$`1`[2,2]-m2$est.bin$`1`[1,2])/sqrt((m2$est.bin$`1`[2,3]^2+m2$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<3.75,])+nrow(temp[temp$rr_scale>=3.75&temp$rr_scale<=4.75,])-2, lower.tail=F))*2
#p=0.82

#Comparing low RR to high RR
t= (m2$est.bin$`1`[3,2]-m2$est.bin$`1`[1,2])/sqrt((m2$est.bin$`1`[3,3]^2+m2$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<3.75,])+nrow(temp[temp$rr_scale>4.75,])-2, lower.tail=F))*2
#p=0.51

#compare direct to indirect
#Medium RR
temp1=d[!is.na(d$Implicit_reject),]
temp2=d[!is.na(d$Explicit_reject),]

#Medium levels of RR
t= (m1$est.bin$`1`[2,2]-m2$est.bin$`1`[2,2])/sqrt((m1$est.bin$`1`[2,3]^2+m2$est.bin$`1`[2,3]^2))
(pt(t,nrow(temp1[temp1$rr_scale>=3.75&temp1$rr_scale<=4.5,])+nrow(temp2[temp2$rr_scale>=3.75&temp2$rr_scale<=4.75,])-2, lower.tail=F))*2
#p=0.12

#High RR
t= (m1$est.bin$`1`[3,2]-m2$est.bin$`1`[3,2])/sqrt((m1$est.bin$`1`[3,3]^2+m2$est.bin$`1`[3,3]^2))
(pt(t,nrow(temp1[temp1$rr_scale>4.5,])+nrow(temp2[temp2$rr_scale>4.75,])-2, lower.tail=F))*2
#p=0.25

####Insensitivity analyses####
#See appendix R file for analyses and figures
mean(d$mayor_insensitiveR[!is.na(d$Implicit_reject)], na.rm=T)
mean(d$mayor_insensitiveR[!is.na(d$Explicit_reject)], na.rm=T)
t.test(d$mayor_insensitiveR[!is.na(d$Implicit_reject)], d$mayor_insensitiveR[!is.na(d$Explicit_reject)])

####Comparisons to the control group####
#See appendix R file for analyses and figures
#Control group compared to indirect condition:
t.test(d$mayor_evalR[d$treatment==0], d$mayor_evalR[d$treatment==2] )
#Control group compared to indirect + denial condition:
t.test(d$mayor_evalR[d$treatment==0], d$mayor_evalR[d$treatment==3] )
#Control group compared to direct condition:
t.test(d$mayor_evalR[d$treatment==0], d$mayor_evalR[d$treatment==4] )
#Control group compared to direct + denial condition:
t.test(d$mayor_evalR[d$treatment==0], d$mayor_evalR[d$treatment==5] )

###### STUDY 2 #######
####Load in the data####
load("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Data/Lucid Partisanship/Study 2 Data.RData")

####Median time spent on the treatments####
summary(whites$treat_time)
####Median time spent on the survey####
summary((whites$`Duration (in seconds)`)/60)

####Basic treatment effects####
#indirect vs. indirect + denial
t1=t.test(as.numeric(mayor_eval) ~ as.numeric(implicit_vs_denial), data = whites)
# MAYOR INSENSITIVE
t2=t.test(as.numeric(mayor_insensitive) ~ as.numeric(implicit_vs_denial), data = whites)
# MAYOR VOTE
t3=t.test(as.numeric(mayor_vote) ~ as.numeric(implicit_vs_denial), data = whites)
# MAYOR FT
t4=t.test(as.numeric(mayor_ft_72) ~ as.numeric(implicit_vs_denial), data = whites)

## EMBRACE
t5=t.test(as.numeric(mayor_eval) ~ as.numeric(implicit_vs_embrace), data = whites)
# MAYOR INSENSITIVE
t6=t.test(as.numeric(mayor_insensitive) ~ as.numeric(implicit_vs_embrace), data = whites)
# MAYOR VOTE
t7=t.test(as.numeric(mayor_vote) ~ as.numeric(implicit_vs_embrace), data = whites)
# MAYOR FT
t8=t.test(as.numeric(mayor_ft_72) ~ as.numeric(implicit_vs_embrace), data = whites)

#Embrace vs. denial
t.test(as.numeric(mayor_eval) ~ as.numeric(denial_vs_embrace), data = whites)
# MAYOR INSENSITIVE
t6=t.test(as.numeric(mayor_insensitive) ~ as.numeric(implicit_vs_embrace), data = whites)
# MAYOR VOTE
t.test(as.numeric(mayor_vote) ~ as.numeric(denial_vs_embrace), data = whites)
# MAYOR FT
t.test(as.numeric(mayor_ft_72) ~ as.numeric(denial_vs_embrace), data = whites)

####FIGURE 3####
t <- data.frame(treatment=c("Denial", "Denial", "Denial", "Denial",
                            "Appropriation", "Appropriation", "Appropriation", "Appropriation"),
                var=c("Mayor's proposal", "Mayor's insensitivity", "Vote", "FT",
                      "Mayor's proposal","Mayor's insensitivity","Vote", "FT"),
                dif=c(t1$estimate[2]-t1$estimate[1],
                      t2$estimate[2]-t2$estimate[1],
                      t3$estimate[2]-t3$estimate[1],
                      t4$estimate[2]-t4$estimate[1],
                      t5$estimate[2]-t5$estimate[1],
                      t6$estimate[2]-t6$estimate[1],
                      t7$estimate[2]-t7$estimate[1],
                      t8$estimate[2]-t8$estimate[1]
                ), 
                se=c(
                  t1$stderr,
                  t2$stderr,
                  t3$stderr,
                  t4$stderr,
                  t5$stderr,
                  t6$stderr,
                  t7$stderr,
                  t8$stderr
                )
)

t$treatment=factor(t$treatment, levels=c("Denial", "Appropriation"))
t$var=factor(t$var, levels=c("Mayor's proposal", "Mayor's insensitivity",
                             "Vote", "FT"))

plot1=ggplot(data = t[t$var=="Mayor's proposal",], aes(x=treatment, y=dif, ymin=I(dif-(1.96*se)), ymax=I(dif+(1.96*se)), color=treatment))+
  geom_pointrange(size=1.5, fatten=2, aes(shape=treatment)) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="red") +
  scale_color_manual(values=cbbPalette)+
  theme_classic()+
  theme(axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=14),
        axis.title.y = element_text(size=14),
        plot.title = element_text(size=12))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.position="bottom")+
  theme(legend.key.size = unit(3,"line"))+
  theme(legend.title = element_blank())+
  theme(plot.caption.position = "plot",
        plot.caption = element_text(hjust = 0)) +
  coord_cartesian(ylim = c(-0.10, 0.10))+
  guides(color=guide_legend(keywidth = 2, keyheight = 5))+
  guides(color = guide_legend(nrow = 1))+
  labs(title="", 
       y= "Effect over indirect message", 
       x="",
       caption = "       Graph presents treatment effects for the denial and appropriation messages, along 
       with 95 percent confidence intervals. The dependent variable is support for the Mayor's 
       proposed policy, which is a 7 point likert item scaled from 0 (least support) to 1 (most
       support). N is 553 and 561 for the denial and appropriation conditions (respectively)")

p=plot1+ggtitle("Support for Mayor's proposal, 2020")
p
setwd("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Final POBE graphs")
ggsave("Figure 3, 2020 Denial - Basic treatment effects.jpeg", width=5, height=6, units="in", dpi=600)

####Specific p-values from figure 3####
#Denial treatment effect
t.test(as.numeric(mayor_eval) ~ as.numeric(implicit_vs_denial), data = whites)
#Appropriation treatment effect
t.test(as.numeric(mayor_eval) ~ as.numeric(implicit_vs_embrace), data = whites)
#Denial compared to appropriation
t.test(as.numeric(mayor_eval) ~ as.numeric(denial_vs_embrace), data = whites)

####Figure 4#####
plot1=ggplot(data = t[t$var=="Vote"|t$var=="FT",], aes(x=treatment, y=dif, ymin=I(dif-(1.96*se)), ymax=I(dif+(1.96*se)), color=treatment))+
  geom_pointrange(size=1.5, fatten=2, aes(shape=treatment)) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="red") +
  scale_color_manual(values=cbbPalette)+
  theme_classic()+
  theme(axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=14),
        axis.title.y = element_text(size=14),
        plot.title = element_text(size=12))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.position="bottom")+
  theme(legend.key.size = unit(3,"line"))+
  theme(legend.title = element_blank())+
  theme(plot.caption.position = "plot",
        plot.caption = element_text(hjust = 0)) +
  coord_cartesian(ylim = c(-0.10, 0.10))+
  guides(color=guide_legend(keywidth = 2, keyheight = 5))+
  guides(color = guide_legend(nrow = 1))+
  labs(title="", 
       y= "Effect over indirect message", 
       x="",
       caption = "       Graph presents treatment effects for the denial and appropriation messages, along with 95 percent confidence intervals. The dependent 
       variables are likelihood of vote for the Mayor, which is a 5 point likert item scaled from 0 (very unlikely) to 1 (very likely), and feeling 
       thermometer ratings of the Mayor, which are scaled from 0 (most negative) to 1 (most positive). N is 553 and 561 for the denial and 
       appropriation conditions (respectively)")

p=plot1+facet_wrap(vars(var))+theme(strip.text.x = element_text(size = 12))+theme(strip.background = element_blank())+
  ggtitle("Vote and feeling thermometer effects, 2020")
p
setwd("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Final POBE graphs")
ggsave("Figure 4, 2020 Denial - Vote and FT effects.jpeg", width=8, height=6, units="in", dpi=600)

####Specific p-values from Figure 4####
#Vote likelihood
#Denial treatment effect
t.test(as.numeric(mayor_vote) ~ as.numeric(implicit_vs_denial), data = whites)
#Appropriation treatment effect
t.test(as.numeric(mayor_vote) ~ as.numeric(implicit_vs_embrace), data = whites)
#Denial compared to appropriation
t.test(as.numeric(mayor_vote) ~ as.numeric(denial_vs_embrace), data = whites)

#Feeling thermometer
#Denial treatment effect
t.test(as.numeric(mayor_ft_72) ~ as.numeric(implicit_vs_denial), data = whites)
#Appropriation treatment effect
t.test(as.numeric(mayor_ft_72) ~ as.numeric(implicit_vs_embrace), data = whites)
#Denial compared to appropriation
t.test(as.numeric(mayor_ft_72) ~ as.numeric(denial_vs_embrace), data = whites)

#####FIGURE 5, Denial moderation by RR####
temp=whites[!is.na(whites$implicit_vs_denial),]
m0<-interflex("binning", Y = "mayor_eval", D = "implicit_vs_denial", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")
m1<-interflex("binning", Y = "mayor_vote", D = "implicit_vs_denial", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")
temp$mayor_ft_72=temp$mayor_ft_7/100
m2<-interflex("binning", Y = "mayor_ft_72", D = "implicit_vs_denial", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")

Diff=c(m0$est.bin$`1`[1,2],
       m0$est.bin$`1`[2,2],
       m0$est.bin$`1`[3,2],
       m1$est.bin$`1`[1,2],
       m1$est.bin$`1`[2,2],
       m1$est.bin$`1`[3,2],
       m2$est.bin$`1`[1,2],
       m2$est.bin$`1`[2,2],
       m2$est.bin$`1`[3,2]
)

se=c(m0$est.bin$`1`[1,3],
     m0$est.bin$`1`[2,3],
     m0$est.bin$`1`[3,3],
     m1$est.bin$`1`[1,3],
     m1$est.bin$`1`[2,3],
     m1$est.bin$`1`[3,3],
     m2$est.bin$`1`[1,3],
     m2$est.bin$`1`[2,3],
     m2$est.bin$`1`[3,3]
)


rr=c("Low RR", "Medium RR", "High RR",
     "Low RR", "Medium RR", "High RR",
     "Low RR", "Medium RR", "High RR"
)

t1=data.frame(cbind(Diff, rr))
t1$se=as.numeric(se)
t1$Diff=as.numeric(Diff)



t1$Group=c( "Approval", "Approval", "Approval",
            "Vote", "Vote", "Vote",
            "FT", "FT", "FT"
)
t1$Group=factor(t1$Group, levels=c("Approval", "Vote", "FT"))

t1$rr=factor(t1$rr, levels=c(
  "Low RR", "Medium RR", "High RR"
))

#These commands do the actual graphing
plot1=ggplot(data = t1, aes(x=Group, y=Diff, ymin=I(Diff-(1.96*se)), ymax=I(Diff+(1.96*se)), color=Group))+
  geom_pointrange(size=1.5, fatten=2, aes(shape=Group)) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="red") +
  scale_color_manual(values=cbbPalette)+
  theme_classic()+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_text(size=14),
        axis.title.y = element_text(size=14),
        plot.title = element_text(size=12))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.position="bottom")+
  theme(legend.key.size = unit(3,"line"))+
  theme(legend.title = element_blank())+
  theme(plot.caption.position = "plot",
        plot.caption = element_text(hjust = 0)) +
  coord_cartesian(ylim = c(-0.3, 0.3))+
  scale_y_continuous(breaks=c(-0.2,-0.1,0,0.1, 0.2))+
  guides(color=guide_legend(keywidth = 2, keyheight = 5))+
  guides(color = guide_legend(nrow = 1))+
  labs(title="", 
       y= "Effect of racial denial", 
       x="",
       caption = "       Graph presents treatment effects for the denial messages over indirect messages, along with 95 percent confidence intervals. The dependent 
       variables are support for the Mayor's proposed policy, which is a 7 point likert item scaled from 0 (least support) to 1 (most support), 
       likelihood of vote for the Mayor, which is a 5 point likert item scaled from 0 (very unlikely) to 1 (very likely), and feeling thermometer 
       ratings of the Mayor, which are scaled from 0 (most negative) to 1 (most positive). N is those low in racial resentment is 374. For those
       with medium levels of racial resentment, N is 345; N for those high in racial resentment is 388.")
p=plot1+facet_wrap(vars(rr))+theme(strip.text.x = element_text(size = 12))+theme(panel.spacing=unit(.05, "lines"),
                                                                                 panel.border = element_rect(color = "black", fill = NA, size = 1), 
                                                                                 strip.background = element_rect(color = "black", size = 1))+
  ggtitle("Denial effects, 2020")
p

setwd("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Final POBE graphs")
ggsave("Figure 5, 2020 Denial by RR.jpeg", width=8, height=6, units="in", dpi=600)

#####Specific p-values from Figure 5:####
#Approval
temp=whites[!is.na(whites$implicit_vs_denial),]
t= (m0$est.bin$`1`[2,2]-m0$est.bin$`1`[1,2])/sqrt((m0$est.bin$`1`[2,3]^2+m0$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])-2, lower.tail=F))*2
#p=0.30
t= (m0$est.bin$`1`[3,2]-m0$est.bin$`1`[1,2])/sqrt((m0$est.bin$`1`[3,3]^2+m0$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.36
t= (m0$est.bin$`1`[3,2]-m0$est.bin$`1`[2,2])/sqrt((m0$est.bin$`1`[3,3]^2+m0$est.bin$`1`[2,3]^2))
(pt(t,nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=T))*2
#p=0.86

#Vote
t= (m1$est.bin$`1`[2,2]-m1$est.bin$`1`[1,2])/sqrt((m1$est.bin$`1`[2,3]^2+m1$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])-2, lower.tail=F))*2
#p=0.40
t= (m1$est.bin$`1`[3,2]-m1$est.bin$`1`[1,2])/sqrt((m1$est.bin$`1`[3,3]^2+m1$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.05
t= (m1$est.bin$`1`[3,2]-m1$est.bin$`1`[2,2])/sqrt((m1$est.bin$`1`[3,3]^2+m1$est.bin$`1`[2,3]^2))
(pt(t,nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.36

#FT
t= (m2$est.bin$`1`[2,2]-m2$est.bin$`1`[1,2])/sqrt((m2$est.bin$`1`[2,3]^2+m2$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])-2, lower.tail=F))*2
#p=0.49
t= (m2$est.bin$`1`[3,2]-m2$est.bin$`1`[1,2])/sqrt((m2$est.bin$`1`[3,3]^2+m2$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.024
t= (m2$est.bin$`1`[3,2]-m2$est.bin$`1`[2,2])/sqrt((m2$est.bin$`1`[3,3]^2+m2$est.bin$`1`[2,3]^2))
(pt(t,nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.262

##### Figure 6 appropriation, Moderation by RR ####
temp=whites[!is.na(whites$implicit_vs_embrace),]
m0<-interflex("binning", Y = "mayor_eval", D = "implicit_vs_embrace", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")
m1<-interflex("binning", Y = "mayor_insensitive", D = "implicit_vs_embrace", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")
m2<-interflex("binning", Y = "mayor_vote", D = "implicit_vs_embrace", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")
temp$mayor_ft_72=temp$mayor_ft_7/100
m3<-interflex("binning", Y = "mayor_ft_72", D = "implicit_vs_embrace", X = "rr_scale", data = as.data.frame(temp),
              na.rm=T, vartype="simu")

Diff=c(m0$est.bin$`1`[1,2],
       m0$est.bin$`1`[2,2],
       m0$est.bin$`1`[3,2],
       m1$est.bin$`1`[1,2],
       m1$est.bin$`1`[2,2],
       m1$est.bin$`1`[3,2],
       m2$est.bin$`1`[1,2],
       m2$est.bin$`1`[2,2],
       m2$est.bin$`1`[3,2],
       m3$est.bin$`1`[1,2],
       m3$est.bin$`1`[2,2],
       m3$est.bin$`1`[3,2]
)

se=c(m0$est.bin$`1`[1,3],
     m0$est.bin$`1`[2,3],
     m0$est.bin$`1`[3,3],
     m1$est.bin$`1`[1,3],
     m1$est.bin$`1`[2,3],
     m1$est.bin$`1`[3,3],
     m2$est.bin$`1`[1,3],
     m2$est.bin$`1`[2,3],
     m2$est.bin$`1`[3,3],
     m3$est.bin$`1`[1,3],
     m3$est.bin$`1`[2,3],
     m3$est.bin$`1`[3,3]
)


rr=c("Low RR", "Medium RR", "High RR",
     "Low RR", "Medium RR", "High RR",
     "Low RR", "Medium RR", "High RR",
     "Low RR", "Medium RR", "High RR"
)

t1=data.frame(cbind(Diff, rr))
t1$se=as.numeric(se)
t1$Diff=as.numeric(Diff)



t1$Group=c( "Approval", "Approval", "Approval",
            "Insens.", "Insens.", "Insens.",
            "Vote", "Vote", "Vote",
            "FT", "FT", "FT"
)
t1$Group=factor(t1$Group, levels=c("Insens.", "Approval", "Vote", "FT"))

t1$rr=factor(t1$rr, levels=c(
  "Low RR", "Medium RR", "High RR"
))


plot1=ggplot(data = t1[t1$Group!="Insens.",], aes(x=Group, y=Diff, ymin=I(Diff-(1.96*se)), ymax=I(Diff+(1.96*se)), color=Group))+
  geom_pointrange(size=1.5, fatten=2, aes(shape=Group)) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="red") +
  scale_color_manual(values=cbbPalette)+
  theme_classic()+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_text(size=14),
        axis.title.y = element_text(size=14),
        plot.title = element_text(size=12))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.position="bottom")+
  theme(legend.key.size = unit(3,"line"))+
  theme(legend.title = element_blank())+
  theme(plot.caption.position = "plot",
        plot.caption = element_text(hjust = 0)) +
  coord_cartesian(ylim = c(-0.3, 0.3))+
  scale_y_continuous(breaks=c(-0.2,-0.1,0,0.1, 0.2))+
  guides(color=guide_legend(keywidth = 2, keyheight = 5))+
  guides(color = guide_legend(nrow = 1))+
  labs(title="", 
       y= "Effect of racial appropriation", 
       x="",
       caption = "       Graph presents treatment effects for appropriations over indirect messages, along with 95 percent confidence intervals. The dependent 
       variables are support for the Mayor's proposed policy, which is a 7 point likert item scaled from 0 (least support) to 1 (most support), 
       likelihood of vote for the Mayor, which is a 5 point likert item scaled from 0 (very unlikely) to 1 (very likely), and feeling thermometer 
       ratings of the Mayor, which are scaled from 0 (most negative) to 1 (most positive). N is those low in racial resentment is 401. For those  
       with medium levels of racial resentment, N is 328; N for those high in racial resentment is 384.")
p=plot1+facet_wrap(vars(rr))+theme(strip.text.x = element_text(size = 12))+theme(panel.spacing=unit(.05, "lines"),
                                                                                 panel.border = element_rect(color = "black", fill = NA, size = 1), 
                                                                                 strip.background = element_rect(color = "black", size = 1))+
  ggtitle("Appropriation effects, 2020")
p

setwd("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Final POBE graphs")
ggsave("Figure 6, 2020 Appropriation by RR.jpeg", width=8, height=6, units="in", dpi=600)

####Specific p-values from Figure 6####
#Approval
temp=whites[!is.na(whites$implicit_vs_embrace),]
t= (m0$est.bin$`1`[2,2]-m0$est.bin$`1`[1,2])/sqrt((m0$est.bin$`1`[2,3]^2+m0$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])-2, lower.tail=T))*2
#p=0.55
t= (m0$est.bin$`1`[3,2]-m0$est.bin$`1`[1,2])/sqrt((m0$est.bin$`1`[3,3]^2+m0$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.70
t= (m0$est.bin$`1`[3,2]-m0$est.bin$`1`[2,2])/sqrt((m0$est.bin$`1`[3,3]^2+m0$est.bin$`1`[2,3]^2))
(pt(t,nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.33

#Vote
t= (m2$est.bin$`1`[2,2]-m2$est.bin$`1`[1,2])/sqrt((m2$est.bin$`1`[2,3]^2+m2$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])-2, lower.tail=T))*2
#p=0.50
t= (m2$est.bin$`1`[3,2]-m2$est.bin$`1`[1,2])/sqrt((m2$est.bin$`1`[3,3]^2+m2$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.22
t= (m2$est.bin$`1`[3,2]-m2$est.bin$`1`[2,2])/sqrt((m2$est.bin$`1`[3,3]^2+m2$est.bin$`1`[2,3]^2))
(pt(t,nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.097

#FT
t= (m3$est.bin$`1`[2,2]-m3$est.bin$`1`[1,2])/sqrt((m3$est.bin$`1`[2,3]^2+m3$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])-2, lower.tail=T))*2
#p=0.31
t= (m3$est.bin$`1`[3,2]-m3$est.bin$`1`[1,2])/sqrt((m3$est.bin$`1`[3,3]^2+m3$est.bin$`1`[1,3]^2))
(pt(t,nrow(temp[temp$rr_scale<=3.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.06
t= (m3$est.bin$`1`[3,2]-m3$est.bin$`1`[2,2])/sqrt((m3$est.bin$`1`[3,3]^2+m3$est.bin$`1`[2,3]^2))
(pt(t,nrow(temp[temp$rr_scale>3.5&temp$rr_scale<4.5,])+nrow(temp[temp$rr_scale>=4.5,])-2, lower.tail=F))*2
#p=0.01


####Insensitivity in Study 2####
#See Appendix file for detailed results
mean(whites$mayor_insensitive[whites$treatment==0], na.rm=T)
mean(whites$mayor_insensitive[whites$treatment==1], na.rm=T)
mean(whites$mayor_insensitive[whites$treatment==2], na.rm=T)

####Respondents of color####
#See Appendix file for analyses

####Partisanship robustness checks####
#See Appendix file for analyses and figures

#Partisanship and racial resentment correlation for the studies:
#Study 1
#Reload the Study 1 data:
load("C:/Users/busby89/Google Drive/Explicit and Implicit messaging/Data/Lucid Denial experiment/Study 1.RData")
cor.test(d$rr_scale[d$pid!=8], d$pid[d$pid!=8])
#Average racial resentment scores for Republicans and Democrats
d$pid3[d$pid<4]=1
d$pid3[d$pid==4|d$pid==8]=2
d$pid3[d$pid>4&d$pid<8]=3
t.test(rr_scale~pid3, data=d[d$pid3!=2,])

#Study 2:
#The whites data object still works for the 2020 data; no need to reload those data
cor.test(whites$rr_scale[whites$pid!=8], whites$pid[whites$pid!=8])
whites$pid3[whites$pid<4]=1
whites$pid3[whites$pid==4|whites$pid==8]=2
whites$pid3[whites$pid>4&whites$pid<8]=3
t.test(rr_scale~pid3, data=whites[whites$pid3!=2,])